package com.cskaoyan.service;

import com.cskaoyan.bean.param.BaseParam;


import com.cskaoyan.bean.param.OrderParam;
import com.cskaoyan.bean.po.OrderPO.OrderGoodsPO;
import com.cskaoyan.bean.po.wxPo.MarketOrder;
import com.cskaoyan.bean.po.wxPo.MarketOrderExample;
import com.cskaoyan.bean.vo.adminOrder.*;
import com.cskaoyan.bean.vo.vxVo.WxHomePageVO;
import com.cskaoyan.bean.vo.vxVo.WxIndexVO;
import com.cskaoyan.mapper.OrderMapper;
import com.cskaoyan.mapper.wxMapper.MarketOrderGoodsMapper;
import com.cskaoyan.mapper.wxMapper.MarketOrderMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    MarketOrderMapper marketOrderMapper;

    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;



    @Override
    public OrderData query(BaseParam param, Integer [] orderStatusArray , Integer userId, String orderSn ,Date start ,Date end) {
        // 在执行查询之前，可以开启分页
        // sql语句预编译的过程给你拼接上分页的sql
     //   System.out.println(end);
        PageHelper.startPage(param.getPage(), param.getLimit());
        // 使用的userMapper中并没有提供page和limit
        List<Order> orders = orderMapper.queryAll(param.getSort(), param.getOrder(),orderStatusArray, userId, orderSn ,start,end);
        // 会去获得一些分页信息
        // 上面的查询对应的PageInfo → 把原始查询结果放入到构造方法里
        PageInfo pageInfo = new PageInfo(orders);

        // total是总的数据量 → 是否等于Orders.length?不是 → 指的是如果不分页的情况下最多会查询出来多少条记录
        // 按照上面的查询的查询条件执行的select count(*)
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return OrderData.data(total,pages,param.getLimit(), param.getPage(), orders);
    }



    @Override
    public List<OrderGoods> queryOrderGoods(Integer id) {
        String specifications=null;
        List<OrderGoodsPO> orderGoodsList=orderMapper.queryOrderGoods(id);
        OrderGoods orderGoodsVO=null;
        for (OrderGoodsPO orderGoods : orderGoodsList) {

            specifications = orderGoods.getSpecifications();
            String replace = specifications.replace("\"", "");
            List<String> split = Arrays.asList(replace.substring(1, replace.length() - 1).split(", "));
             orderGoodsVO = new OrderGoods(orderGoods.getId(),orderGoods.getOrderId(),orderGoods.getGoodsId(),orderGoods.getGoodsName(),orderGoods.getGoodsSn(),orderGoods.getProductId(),orderGoods.getNumber(),orderGoods.getPrice(),split,orderGoods.getPicUrl(),orderGoods.getComment(),orderGoods.getAddTime(),orderGoods.getUpdateTime(),orderGoods.isDeleted());
        }
        ArrayList<OrderGoods> orderGoodsVOS = new ArrayList<>();
        orderGoodsVOS.add(orderGoodsVO);
        return orderGoodsVOS;
    }

    @Override
    public User OrderUser(Integer id) {
        User user=orderMapper.queryOrderUsers(id);
        return user;
    }

    @Override
    public Order2 queryOrder(Integer id) {
        Order2 order2=orderMapper.queryOrder(id);
        return order2;
    }

    @Override
    public void updateShip(Integer orderId, String shipChannel, String shipSn) {
        orderMapper.updateshipOrder(orderId,shipChannel,shipSn);
    }

    @Override
    public void updateRefund(Integer orderId, Integer refundMoney) {
        orderMapper.updateRefundOrder(orderId,refundMoney);
    }

    @Override
    public String selectOrderSnByOrderId(Integer orderId) {

       String OrderSn= orderMapper.selectSnById(orderId);
        return OrderSn;
    }

    @Override
    public void update4delete(Integer orderId) {
        orderMapper.updateForDelete( orderId);
    }

    @Override
    public WxIndexVO userIndexList(Integer id) {
        WxHomePageVO wxHomePageVO = new WxHomePageVO();
        Integer count = marketOrderMapper.selectUncommentByUserId(id);
        wxHomePageVO.setUncomment(count);
        wxHomePageVO.setUnpaid(orderMapper.selectCountByCode(id, OrderParam.NO_PAY));
        wxHomePageVO.setUnrecv(orderMapper.selectCountByCode(id, OrderParam.SHIP));
        wxHomePageVO.setUnship(orderMapper.selectCountByCode(id, OrderParam.PAID));
        WxIndexVO wxIndexVO = new WxIndexVO();
        wxIndexVO.setOrder(wxHomePageVO);
        return wxIndexVO;
    }
}
